package com.qianbei.common.net.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.common.net.control.ServerControl.ProgressListener;
import com.qianbei.common.net.control.ServerControl.ServerListener;

public abstract class BaseProgressView extends FrameLayout implements
		ServerListener, ProgressListener {

	private ServerControl sc;
	private ServerListener serverListener;
	public View errorView;

	public BaseProgressView(Context context) {
		this(context, null);
	}

	public BaseProgressView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void startControl(ServerControl sc) {
		this.startControl(sc, "正在加载");
	}

	public void startControl(ServerControl sc, String describe) {
		if (sc == null) {
			return;
		}
		this.sc = sc;
		serverListener = sc.serverListener;
		sc.serverListener = this;
		sc.progressListener = this;

		showProgress(describe);
		startControl();
	}

	public void startControl() {
		if (sc == null) {
			return;
		}
		sc.startVolley();
	}

	@Override
	public void serverFinish(ServerResult sr) {
		serverListener.serverFinish(sr);
		if (sr.exception == null) {
			progressFinish();
		}
	}

	@Override
	public abstract void progressRate(int value);

	public abstract void showProgress(String describe);

	public abstract void showError();

	public abstract void progressFinish();
}
